﻿using Freshbox2._0.BLL;
using Freshbox2._0.MODEL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;

namespace Freshbox2._0.UI.Controllers
{
    public class OrderByTimeEchartController : BaseController<usersorder>
    {
        //
        // GET: /OrderByTimeEchart/

        public override BLL.BaseBll<usersorder> bll
        {
            get
            {
                return new UsersOrderBll();
            }

        }

        public class OrderModel
        {
            /// <summary>
            /// 横坐标 时间
            /// </summary>
            public string Date { get; set; }
            //销售金额
            public string CountMoney { get; set; }
        }
        /// <summary>
        /// 按时间段统计  销售金额
        /// </summary>
        /// <param name="date1"></param>
        /// <param name="date2"></param>
        /// <returns></returns>
        public ActionResult GetOrderAll(string date1,string date2,string storeid)
        {
            List<OrderModel> dataList = new List<OrderModel>();
            // 默认查询本天的所有店铺的 销售情况  
            if (string.IsNullOrEmpty(date1) && string.IsNullOrEmpty(date2)&& string.IsNullOrEmpty(storeid))
            {
                // condition = x => x.orderStatus == 1 && x.orderTime <= DateTime.Now;
                var query = bll.Search().ToList();


                //第二种 改进  0--6小时
                
                for (var i = 0; i <= 24; i = i + 1)
                {
                        OrderModel orderModel = new OrderModel();
                        var countData =i;
                        if (countData == 0)
                        {
                            orderModel.Date = "00:00";
                            orderModel.CountMoney = "0";
                            dataList.Add(orderModel);
                            i = i + 5;  //计算 5+1=6 小时
                        }
                        else   if (countData == 6)
                        {

                            DateTime dt1 = DateTime.Now.Date.AddHours(+countData - 6);   //0 点
                            DateTime dt2 = DateTime.Now.Date.AddHours(+countData);       //6 点
                            //一天的数据   
                            var queryable = query.Where(x => x.orderTime.Hour >= dt1.Hour && x.orderTime.Hour <= dt2.Hour - 1 && x.orderTime.Date == dt1.Date && x.orderStatus == 1);
                            var dd = queryable.ToList();
                            decimal? sellIncome = 0;
                            var sellIncomeList = (from r in dd

                                                  select r.orderSumprice).ToList();
                            foreach (var oDecimal in sellIncomeList)
                            {
                                sellIncome += oDecimal;
                            }
                            orderModel.Date = DateTime.Now.Date.AddHours(+countData).ToString("HH:mm");
                            orderModel.CountMoney = sellIncome.ToString();
                            dataList.Add(orderModel);
                        }
                        else
                        {
                            DateTime dt1 = DateTime.Now.Date.AddHours(+countData-1);   //6 点
                            DateTime dt2 = DateTime.Now.Date.AddHours(+countData);       //7点
                            //一天的数据   
                            var queryable = query.Where(x => x.orderTime.Hour >= dt1.Hour && x.orderTime.Hour <= dt2.Hour - 1 && x.orderTime.Date == dt1.Date && x.orderStatus == 1);
                            var dd = queryable.ToList();
                            decimal? sellIncome = 0;
                            var sellIncomeList = (from r in dd

                                                  select r.orderSumprice).ToList();
                            foreach (var oDecimal in sellIncomeList)
                            {
                                sellIncome += oDecimal;
                            }
                            orderModel.Date = DateTime.Now.Date.AddHours(+countData).ToString("HH:mm");
                            orderModel.CountMoney = sellIncome.ToString();
                            dataList.Add(orderModel);
 
                        }
                }


                //之前的 设计 每隔两个小时
                //for (var i = 0; i <= 24; i = i + 2)
                //{
                //    OrderModel orderModel = new OrderModel();
                //    var countData = i;
                //    if (countData == 0)
                //    {
                //        orderModel.Date = "00:00";
                //        orderModel.CountMoney ="0";
                //        dataList.Add(orderModel);
                //    }
                //    else
                //    {
                        
                //        DateTime dt1 = DateTime.Now.Date.AddHours(+countData-2);  //0 点
                //        DateTime dt2 = DateTime.Now.Date.AddHours(+countData ); //2 点
                //        //一天的数据   
                //        // var queryable = query.Where(u => u.orderTime.Date == DateTime.Now.Date.AddDays(-countData) && u.orderStatus == 1);
                //        // var list=
                //        var queryable = query.Where(x => x.orderTime.Hour >= dt1.Hour && x.orderTime.Hour <= dt2.Hour - 1 && x.orderTime.Date == dt1.Date && x.orderStatus == 1);
                //        var dd = queryable.ToList();
                //        decimal? sellIncome = 0;
                //        var sellIncomeList = (from r in dd

                //                              select r.orderSumprice).ToList();
                //        foreach (var oDecimal in sellIncomeList)
                //        {
                //            sellIncome += oDecimal;
                //        }
                //        orderModel.Date = DateTime.Now.Date.AddHours(+countData).ToString("HH:mm");
                //        orderModel.CountMoney = sellIncome.ToString();
                //        dataList.Add(orderModel);
                //    }
                //}
                var dataArray = dataList.ToArray();
                return Json(dataList, JsonRequestBehavior.AllowGet);

            }
            //根据时间段查询    查询所有的店铺的string
            if (date1 != null && date1 != "" && date2 != null && date2 != ""&&string.IsNullOrEmpty(storeid))
            {
                DateTime datetime1 = DateTime.Parse(date1);
                DateTime datetime2 = DateTime.Parse(date2).AddDays(+1);

                var query = bll.Search().Where(x => x.orderTime >= datetime1 && x.orderTime <= datetime2).ToList();
                for (var i = 0; i <= 24; i = i + 1)
                {
                    OrderModel orderModel = new OrderModel();
                    var countData = i;
                    if (countData == 0)
                    {
                        orderModel.Date = "00:00";
                        orderModel.CountMoney = "0";
                        dataList.Add(orderModel);
                        i = i + 5;  //计算 5+1=6 小时
                    }
                    else if (countData == 6)
                    {
                        DateTime dt1 = datetime1.Date.AddHours(+countData - 6);   //0 点
                        DateTime dt2 = datetime1.Date.AddHours(+countData);       //6 点
                        //一天的数据   
                        var queryable = query.Where(x => x.orderTime.Hour >= dt1.Hour && x.orderTime.Hour <= dt2.Hour - 1 && x.orderStatus == 1);
                     
                        var dd = queryable.ToList();
                        decimal? sellIncome = 0;
                        var sellIncomeList = (from r in dd

                                              select r.orderSumprice).ToList();
                        foreach (var oDecimal in sellIncomeList)
                        {
                            sellIncome += oDecimal;
                        }
                        orderModel.Date = DateTime.Now.Date.AddHours(+countData).ToString("HH:mm");
                        orderModel.CountMoney = sellIncome.ToString();
                        dataList.Add(orderModel);
                    }
                    else
                    {
                        DateTime dt1 = datetime1.Date.AddHours(+countData - 1);   //6 点
                        DateTime dt2 = datetime1.Date.AddHours(+countData);       //7点
                        //一天的数据   
                        var queryable = query.Where(x => x.orderTime.Hour >= dt1.Hour && x.orderTime.Hour <= dt2.Hour - 1 && x.orderStatus == 1);
                        var dd = queryable.ToList();
                        decimal? sellIncome = 0;
                        var sellIncomeList = (from r in dd

                                              select r.orderSumprice).ToList();
                        foreach (var oDecimal in sellIncomeList)
                        {
                            sellIncome += oDecimal;
                        }
                        orderModel.Date = DateTime.Now.Date.AddHours(+countData).ToString("HH:mm");
                        orderModel.CountMoney = sellIncome.ToString();
                        dataList.Add(orderModel);

                    }
                }
                //for (var i = 0; i <= 24; i = i + 2)
                //{
                //    OrderModel orderModel = new OrderModel();
                //    var countData = i;
                //    if (countData == 0)
                //    {
                //        orderModel.Date = "00:00";
                //        orderModel.CountMoney = "0";
                //        dataList.Add(orderModel);
                //    }
                //    else
                //    {
                //        DateTime dt1 = datetime1.Date.AddHours(+countData - 2);  //0 点
                //        int a = dt1.Hour;      //  0
                //        DateTime dt2 = datetime1.Date.AddHours(+countData); //2 点
                //        //一天的数据   
                //        // var queryable = query.Where(u => u.orderTime.Date == DateTime.Now.Date.AddDays(-countData) && u.orderStatus == 1);
                //        // var list=

                //        var queryable = query.Where(x => x.orderTime.Hour >= dt1.Hour && x.orderTime.Hour <= dt2.Hour-1  &&x.orderStatus == 1);
                //        var dd = queryable.ToList();
                //        decimal? sellIncome = 0;
                //        var sellIncomeList = (from r in dd

                //                              select r.orderSumprice).ToList();
                //        foreach (var oDecimal in sellIncomeList)
                //        {
                //            sellIncome += oDecimal;
                //        }
                //        orderModel.Date = DateTime.Now.Date.AddHours(+countData).ToString("HH:mm");
                //        orderModel.CountMoney = sellIncome.ToString();
                //        dataList.Add(orderModel);
                //    }
                //}
                var dataArray = dataList.ToArray();
                return Json(dataList, JsonRequestBehavior.AllowGet);

            }

            //根据时间段查询   和    店铺  查询
            if (date1 != null && date1 != "" && date2 != null && date2 != "" && storeid != null && storeid != "")
            {
                int storeID = Convert.ToInt32(storeid);
                DateTime datetime1 = DateTime.Parse(date1);
                DateTime datetime2 = DateTime.Parse(date2).AddDays(+1);

                var query = bll.Search().Where(x => x.orderTime >= datetime1 && x.orderTime <= datetime2&&x.storeid==storeID).ToList();
                for (var i = 0; i <= 24; i = i + 1)
                {
                    OrderModel orderModel = new OrderModel();
                    var countData = i;
                    if (countData == 0)
                    {
                        orderModel.Date = "00:00";
                        orderModel.CountMoney = "0";
                        dataList.Add(orderModel);
                        i = i + 5;  //计算 5+1=6 小时
                    }
                    else if (countData == 6)
                    {

                        DateTime dt1 = datetime1.Date.AddHours(+countData - 6);   //0 点
                        DateTime dt2 = datetime1.Date.AddHours(+countData);       //6 点
                        //一天的数据   
                        var queryable = query.Where(x => x.orderTime.Hour >= dt1.Hour && x.orderTime.Hour <= dt2.Hour - 1 && x.orderStatus == 1);
                        
                        var dd = queryable.ToList();
                        decimal? sellIncome = 0;
                        var sellIncomeList = (from r in dd

                                              select r.orderSumprice).ToList();
                        foreach (var oDecimal in sellIncomeList)
                        {
                            sellIncome += oDecimal;
                        }
                        orderModel.Date = DateTime.Now.Date.AddHours(+countData).ToString("HH:mm");
                        orderModel.CountMoney = sellIncome.ToString();
                        dataList.Add(orderModel);
                    }
                    else
                    {
                        DateTime dt1 = DateTime.Now.Date.AddHours(+countData - 1);   //6 点
                        DateTime dt2 = DateTime.Now.Date.AddHours(+countData);       //7点
                        //一天的数据   
                        var queryable = query.Where(x => x.orderTime.Hour >= dt1.Hour && x.orderTime.Hour <= dt2.Hour - 1 && x.orderStatus == 1);
                        var dd = queryable.ToList();
                        decimal? sellIncome = 0;
                        var sellIncomeList = (from r in dd

                                              select r.orderSumprice).ToList();
                        foreach (var oDecimal in sellIncomeList)
                        {
                            sellIncome += oDecimal;
                        }
                        orderModel.Date = DateTime.Now.Date.AddHours(+countData).ToString("HH:mm");
                        orderModel.CountMoney = sellIncome.ToString();
                        dataList.Add(orderModel);

                    }
                }
                //for (var i = 0; i <= 24; i = i + 2)
                //{
                //    OrderModel orderModel = new OrderModel();
                //    var countData = i;
                //    if (countData == 0)
                //    {
                //        orderModel.Date = "00:00";
                //        orderModel.CountMoney = "0";
                //        dataList.Add(orderModel);
                //    }
                //    else
                //    {
                //        DateTime dt1 = datetime1.Date.AddHours(+countData - 2);  //0 点
                //        int a = dt1.Hour;      //  0
                //        DateTime dt2 = datetime1.Date.AddHours(+countData); //2 点
                //        //一天的数据   
                //        // var queryable = query.Where(u => u.orderTime.Date == DateTime.Now.Date.AddDays(-countData) && u.orderStatus == 1);
                //        // var list=

                //        var queryable = query.Where(x => x.orderTime.Hour >= dt1.Hour && x.orderTime.Hour <= dt2.Hour - 1 && x.orderStatus == 1);
                //        var dd = queryable.ToList();
                //        decimal? sellIncome = 0;
                //        var sellIncomeList = (from r in dd

                //                              select r.orderSumprice).ToList();
                //        foreach (var oDecimal in sellIncomeList)
                //        {
                //            sellIncome += oDecimal;
                //        }
                //        orderModel.Date = DateTime.Now.Date.AddHours(+countData).ToString("HH:mm");
                //        orderModel.CountMoney = sellIncome.ToString();
                //        dataList.Add(orderModel);
                //    }
                //}
                var dataArray = dataList.ToArray();
                return Json(dataList, JsonRequestBehavior.AllowGet);

            }
            //根据   店铺  查询  默认今天的
            if (string.IsNullOrEmpty(date1) && string.IsNullOrEmpty(date2) && storeid != null && storeid != "")
            {
                int storeID = Convert.ToInt32(storeid);
                DateTime todayNow = DateTime.Now;
                DateTime midnight = DateTime.Now;
                midnight = midnight.Date;
            
                var query = bll.Search().Where(x=>x.storeid==storeID && x.orderTime <= todayNow && x.orderTime >= midnight).ToList();
                for (var i = 0; i <= 24; i = i + 1)
                {
                    OrderModel orderModel = new OrderModel();
                    var countData = i;
                    if (countData == 0)
                    {
                        orderModel.Date = "00:00";
                        orderModel.CountMoney = "0";
                        dataList.Add(orderModel);
                        i = i + 5;  //计算 5+1=6 小时
                    }
                    else if (countData == 6)
                    {

                        DateTime dt1 = DateTime.Now.Date.AddHours(+countData - 6);   //0 点
                        DateTime dt2 = DateTime.Now.Date.AddHours(+countData);       //6 点
                        //一天的数据   

                        var queryable = query.Where(x => x.orderTime.Hour >= dt1.Hour && x.orderTime.Hour <= dt2.Hour - 1 && x.orderTime.Date == dt1.Date && x.orderStatus == 1);
                        var dd = queryable.ToList();
                        decimal? sellIncome = 0;
                        var sellIncomeList = (from r in dd

                                              select r.orderSumprice).ToList();
                        foreach (var oDecimal in sellIncomeList)
                        {
                            sellIncome += oDecimal;
                        }
                        orderModel.Date = DateTime.Now.Date.AddHours(+countData).ToString("HH:mm");
                        orderModel.CountMoney = sellIncome.ToString();
                        dataList.Add(orderModel);
                    }
                    else
                    {
                        DateTime dt1 = DateTime.Now.Date.AddHours(+countData - 1);   //6 点
                        DateTime dt2 = DateTime.Now.Date.AddHours(+countData);       //7点
                        //一天的数据   
                        var queryable = query.Where(x => x.orderTime.Hour >= dt1.Hour && x.orderTime.Hour <= dt2.Hour - 1 && x.orderTime.Date == dt1.Date && x.orderStatus == 1);
                        var dd = queryable.ToList();
                        decimal? sellIncome = 0;
                        var sellIncomeList = (from r in dd

                                              select r.orderSumprice).ToList();
                        foreach (var oDecimal in sellIncomeList)
                        {
                            sellIncome += oDecimal;
                        }
                        orderModel.Date = DateTime.Now.Date.AddHours(+countData).ToString("HH:mm");
                        orderModel.CountMoney = sellIncome.ToString();
                        dataList.Add(orderModel);

                    }
                }
                //
                //for (var i = 0; i <= 24; i = i + 2)
                //{
                //    OrderModel orderModel = new OrderModel();
                //    var countData = i;
                //    if (countData == 0)
                //    {
                //        orderModel.Date = "00:00";
                //        orderModel.CountMoney = "0";
                //        dataList.Add(orderModel);
                //    }
                //    else
                //    {

                //        DateTime dt1 = DateTime.Now.Date.AddHours(+countData - 2);  //0 点
                //        DateTime dt2 = DateTime.Now.Date.AddHours(+countData); //2 点
                //        //一天的数据   
                //        // var queryable = query.Where(u => u.orderTime.Date == DateTime.Now.Date.AddDays(-countData) && u.orderStatus == 1);
                //        // var list=
                //        var queryable = query.Where(x => x.orderTime.Hour >= dt1.Hour && x.orderTime.Hour <= dt2.Hour - 1 && x.orderTime.Date == dt1.Date && x.orderStatus == 1);
                //        var dd = queryable.ToList();
                //        decimal? sellIncome = 0;
                //        var sellIncomeList = (from r in dd

                //                              select r.orderSumprice).ToList();
                //        foreach (var oDecimal in sellIncomeList)
                //        {
                //            sellIncome += oDecimal;
                //        }
                //        orderModel.Date = DateTime.Now.Date.AddHours(+countData).ToString("HH:mm");
                //        orderModel.CountMoney = sellIncome.ToString();
                //        dataList.Add(orderModel);
                //    }
                //}
                var dataArray = dataList.ToArray();
                return Json(dataList, JsonRequestBehavior.AllowGet);
            }
            return Json(dataList, JsonRequestBehavior.AllowGet);
        }

    }
}
